package com.led.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class DBMgr{
	static Connection connection = null;
	static boolean DEBUG = true;
	
	public static Connection getConnect(){
		if(DEBUG){
			return getConnectionLocal();
		} else {
			return getConnectionOnline();
		}
	}
	
	public synchronized static void reset(){
		System.out.println("enter DBMgr:reset");
		try{
			if(null != connection){
				connection.close();
			}
			connection = null;
		} catch (Exception e){
			e.printStackTrace();
		}
	}
	
	private static synchronized Connection getConnectionOnline(){
		if(null != connection){
			return connection;
		}

		try {
			/***** 1. 填写数据库相关信息(请查找数据库详情页) *****/
			String databaseName = " eiZrxDYAgZRYcJuTRljT ";
			String host = "sqld.duapp.com";
			String port = "4050";
			String username = " urtgxzMPVigNEtOQF7yzg7C9";// 用户名(api key);
			String password = "1e0jDqkZ7fUwNgFD5LzwPY4YAQURFGYM";// 密码(secret
																	// key)
			String driverName = "com.mysql.jdbc.Driver";
			String dbUrl = "jdbc:mysql://";
			String serverName = host + ":" + port + "/";
			String connName = dbUrl + serverName + databaseName;

			/****** 2. 接着连接并选择数据库名为databaseName的服务器 ******/
			Class.forName(driverName);
				connection = DriverManager.getConnection(connName, username,
					password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;
	}
	

	private static synchronized Connection getConnectionLocal(){
		if(null != connection){
			return connection;
		}

		try {
			/***** 1. 填写数据库相关信息(请查找数据库详情页) *****/
			String databaseName = "led8";
			String host = "localhost";
			String port = "3306";
			String username = "led8";// 用户名(api key);
			String password = "led8";// 密码(secret, key)
			
			
			String driverName = "com.mysql.jdbc.Driver";
			String dbUrl = "jdbc:mysql://";
			String serverName = host + ":" + port + "/";
			String connName = dbUrl + serverName + databaseName;
			/****** 2. 接着连接并选择数据库名为databaseName的服务器 ******/
			Class.forName(driverName);
				connection = DriverManager.getConnection(connName, username,
					password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;
	}
	
	public static boolean exec(String sql, Object... params){
		Connection con = getConnect();
		try {
			PreparedStatement ps = con.prepareStatement(sql);
			for(int i = 0; i < params.length; i++){
				ps.setObject(i, params[i]);
			}
			ps.execute();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		return false;
	}
	
	private DBMgr() {
		
		Statement stmt = null;
		ResultSet rs = null;
		String sql = null;
		try {

			stmt = connection.createStatement();
			/* 至此连接已完全建立，就可对当前数据库进行相应的操作了 */
			/* 3. 接下来就可以使用其它标准mysql函数操作进行数据库操作 */
			// 创建一个数据库表
			sql = "create table if not exists test_mysql("
					+ "id int primary key auto_increment," + "no int, "
					+ "name varchar(1024)," + "key idx_no(no))";
			stmt.execute(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
